package com.account.core.service.impl;

import com.account.common.utils.DateUtils;
import com.account.core.domain.Account;
import com.account.core.dto.*;
import com.account.core.mapper.AccountMapper;
import com.account.core.service.IAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 账号Service业务层处理
 *
 * @author ylstech
 * @date 2025-07-07
 */
@Slf4j
@Service
public class AccountServiceImpl implements IAccountService {

    @Resource
    private AccountMapper accountMapper;
    @Resource
    private AccountAPI accountAPI;

    /**
     * 查询账号
     *
     * @param id 账号主键
     * @return 账号
     */
    @Override
    public Account selectAccountById(Long id) {
        return accountMapper.selectAccountById(id);
    }

    /**
     * 批量查询账号
     *
     * @param ids 需要删除的账号主键集合
     * @return 账号集合
     */
    public List<Account> selectAccountByIds(Long[] ids) {
        return accountMapper.selectAccountListByIds(ids);
    }

    /**
     * 查询账号列表
     *
     * @param account 账号
     * @return 账号
     */
    @Override
    public List<Account> selectAccountList(Account account) {
        return accountMapper.selectAccountList(account);
    }

    /**
     * 新增账号
     *
     * @param req 账号
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertAccount(UserCreateRequest req) {
        if (ObjectUtils.isEmpty(req.getCount()) || req.getCount() <= 1) {
            ResponseEntity<ApiResponse<String>> res = accountAPI.createUser(req);
        } else {
            List<UserCreateRequest> users = new ArrayList<>();

            String prefix = DateUtils.dateTimeNow();
            for (long i = 1; i <= req.getCount(); i++) {
                UserCreateRequest newReq = new UserCreateRequest();
                BeanUtils.copyProperties(req, newReq);

                if ("TEACHER".equals(newReq.getUserType())) {
                    newReq.setFirstName("t" + prefix + i);
                } else {
                    newReq.setFirstName("s" + prefix + i);
                }
                newReq.setLastName(newReq.getUsername());
                newReq.setUsername(newReq.getUsername() + newReq.getFirstName() + i);
                if ("STUDENT".equals(newReq.getUserType())) {
                    newReq.setEmail(newReq.getUsername() + "@" + newReq.getTeacherCode() + ".com");
                } else {
                    newReq.setEmail(newReq.getUsername() + "@" + newReq.getLastName() + ".com");
                }

                users.add(newReq);
            }
            BatchCreateUsersRequest batchReq = new BatchCreateUsersRequest();
            batchReq.setUsers(users);
            ResponseEntity<ApiResultsResponse<List<ApiResponse<String>>>> res = accountAPI.batchCreateUsers(batchReq);
        }
    }

    /**
     * 修改账号
     *
     * @param account 账号
     * @return 结果
     */
    @Override
    public void updateAccount(Account account) {
        accountMapper.updateAccount(account);
    }

    /**
     * 修改密码
     *
     * @param req
     * @return 结果
     */
    public void resetPassword(UpdatePasswordRequest req) {
        accountAPI.updatePassword(req);
    }

    /**
     * 批量删除账号
     *
     * @param ids 需要删除的账号主键
     * @return 结果
     */
    @Override
    public void deleteAccountByIds(String[] ids) {
        BatchDeleteUsersRequest req = new BatchDeleteUsersRequest();
        req.setUserIds(Arrays.asList(ids));
        accountAPI.batchDeleteUsers(req);
    }

    /**
     * 删除账号信息
     *
     * @param id 账号主键
     * @return 结果
     */
    @Override
    public void deleteAccountById(String id) {
    }

}
